package com.axjy.service.impl;

import com.axjy.mapper.PermissionMapper;
import com.axjy.mapper.RolePermissionMapper;
import com.axjy.pojo.Permission;
import com.axjy.pojo.RolePermission;
import com.axjy.service.PermissionService;
import com.axjy.vo.TreeNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {


    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    private int count(Integer id){
        QueryWrapper<Permission> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        return permissionMapper.selectCount(wrapper);
    }

    private List<TreeNode> search(Integer roleId,Integer parentId){
        QueryWrapper<Permission> wrapper= new QueryWrapper<>();
        if(parentId==null){
            wrapper.isNull("parent_id");
        }else{
            wrapper.eq("parent_id",parentId);
        }
        List<Permission> list= permissionMapper.selectList(wrapper);

          List<TreeNode> nodes=  list.stream().map(permission -> {
                TreeNode node=new TreeNode();
                node.setId(permission.getId());
                node.setTitle(permission.getName());

                int count=count(node.getId());

                if(count==0) {
                    QueryWrapper<RolePermission> queryWrapper=new QueryWrapper<>();
                    queryWrapper.eq("role_id",roleId);
                    queryWrapper.eq("perm_id",node.getId());
                    int count2=rolePermissionMapper.selectCount(queryWrapper);
                    if(count2>0) {
                        node.setChecked(true);
                    }
                }

                return node;
          }).collect(Collectors.toList());
        return nodes;

    }


    @Override
    public List<TreeNode> search4Tree(Integer roleId) {
        List<TreeNode>  list= search(roleId,null);
        if(list!=null&&list.size()>0) {
            for (TreeNode node01 : list) {
                Integer parentId = node01.getId();
                List<TreeNode> list02 = search(roleId,parentId);
                node01.getChildren().addAll(list02);
                 if(list02!=null&&list.size()>0){
                     for(TreeNode node02:list02){
                         List<TreeNode> list03=search(roleId,node02.getId());
                         node02.getChildren().addAll(list03);
                     }
                 }
            }
        }
        return list;
    }

    @Override
    public void grant(Integer roleId, Integer[] ids) {
        QueryWrapper<RolePermission> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        rolePermissionMapper.delete(wrapper);
        if(ids!=null&&ids.length>0){
            for(Integer id:ids){
                rolePermissionMapper.insert(new RolePermission(roleId,id));
            }
        }
    }
}
